import { resolve } from 'node:path';
import { defineConfig, loadEnv } from 'vite';
import type { UserConfig } from 'vite';
import createVitePlugins from './build/vite/plugins';
import proxy from './build/vite/proxy';

// https://vitejs.dev/config/
export default defineConfig(({mode}): UserConfig => {
  const root = process.cwd();
      const env = loadEnv(mode, root);
      console.log(env);
  const isBuild = process.env.NODE_ENV === 'production';
  return {
    resolve: {
      // https://cn.vitejs.dev/config/#resolve-alias
      alias: {
        // 设置别名
        '@': resolve(__dirname, './src'),
      },
    },
    // vite 相关配置
    server: {
      host: '0.0.0.0', //ip地址
      port: env.VITE_PORT, //端口号
      open: env.VITE_OPEN === 'true' ? true : false, //启动后是否自动打开浏览器
      proxy: {
          [env.VITE_APP_API]: {
              target: env.VITE_APP_URL,	//后台地址
              changeOrigin: true, //开启跨域
              rewrite: (path) => path.replace(/^\/zbcg/, '')
          },
      }
    },
    plugins: createVitePlugins(isBuild),
  };
});
